import { on, UE_ALREADY_3D, CESIUM_VIEWER_INITIALIZATION } from './utils/EventBus.js'
import { createButtonGroup } from './utils/create-temp.js'
import * as Cesium from 'cesium'
import { waitUntilValueExist, saveJsonFile } from './utils/unsorted.js'

export const setup = () => {
  // document.body.style.backgroundColor = '#000'
}

export const f1 = () => {
  createButtonGroup([
    {
      text: 'camera',
      click: () => {
        // const rectangle = cesiumViewer.camera.computeViewRectangle()
        console.log('rectangle: ', cesiumViewer.camera)
        // {
        //     "west": -3.141592653589793,
        //     "south": -1.5707963267948966,
        //     "east": 3.141592653589793,
        //     "north": 1.5707963267948966
        // }
      },
    },
  ])
}

let downloadData = []
export function download() {
  createButtonGroup([
    {
      text: '下载json',
      click: () => {
        return saveJsonFile(downloadData)
      },
    },
  ])
}

// 临时
export function tempGetCoordinates() {
  waitUntilValueExist(() => window.cesiumViewer).then((viewer) => {
    viewer.scene.globe.depthTestAgainstTerrain = true
    const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
    handler.setInputAction(function (movement) {
      // movement.position 为屏幕坐标
      // 从相机位置到windowPosition 世界坐标中的像素创建射线。
      // 返回Cartesian3射线的位置和方向。
      const ray = viewer.camera.getPickRay(movement.position)
      // 查找射线与渲染的地球表面之间的交点。射线必须以世界坐标给出。
      const intersectionCartesian3 = viewer.scene.globe.pick(ray, viewer.scene)
      const pickPosition = viewer.scene.pickPosition(movement.position)
      const pickEllipsoid = viewer.camera.pickEllipsoid(
        movement.position,
        viewer.scene.globe.ellipsoid,
      )

      downloadData.push(Cesium.Cartographic.fromCartesian(pickPosition))

      console.log('viewer.scene.pickPosition : ', Cesium.Cartographic.fromCartesian(pickPosition))
      console.log(
        'viewer.scene.globe.pick ray',
        Cesium.Cartographic.fromCartesian(intersectionCartesian3),
      )
      console.log('--------------------')
      viewer.entities.add({
        position: pickPosition,
        point: {
          pixelSize: 10,
          color: Cesium.Color.RED,
        },
      })
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  })
}

export const loadAnQing3Dtileset = async (viewer = window.cesiumViewer) => {
  let tileset
  try {
    tileset = await Cesium.Cesium3DTileset.fromUrl(
      'http://127.0.0.1:4090/tileset(3e4dd6362cec60c7f98b1e44527d0ae8).json',
    )
    viewer.scene.primitives.add(tileset)
    console.log('tileset: ', tileset)
    viewer.scene.globe.depthTestAgainstTerrain = true
    viewer.zoomTo(
      tileset,
      new Cesium.HeadingPitchRange(0.0, -0.5, tileset.boundingSphere.radius * 2.0),
    )
  } catch (error) {
    console.log(`Error loading tileset: ${error}`)
  }
}

export const monitorCameraEvent = () => {
  // cesiumViewer.camera.percentageChanged = 0.5

  cesiumViewer.camera.moveStart.addEventListener((moveStart) => {
    // console.log('moveStart: ', moveStart)
  })

  cesiumViewer.camera.changed.addEventListener((changed) => {
  })
  cesiumViewer.camera.moveEnd.addEventListener((moveEnd) => {
    // console.log('cesiumViewer.camera.pitch: ', Cesium.Math.toDegrees(cesiumViewer.camera.pitch))
    // console.log('cesiumViewer.camera.pitch: ', cesiumViewer.camera.pitch);
    // console.log('cesiumViewer.camera.heading: ', Cesium.Math.toDegrees(cesiumViewer.camera.heading))
    // console.log('cesiumViewer.camera.roll: ', cesiumViewer.camera.roll);

  })

}
